import sys
from PyQt5.QtCore import *  # type: ignore
from PyQt5.QtGui import *  # type: ignore
from PyQt5.QtWidgets import *  # type: ignore

from traits.api import *
from traitsui.api import *

import mywidget
import threading

class C1(HasTraits):
    a = Str('fjdkslfdsf')
    b = Str('fjdkslfdsf   1')
    button = Button('test')

    def _button_fired(self):
        print('this is button')

    traits_view = View(
        'a',
        'b',
        'button',
    )


class C2(HasTraits):
    a = Str('fjdkslfdsf')
    b = Str('fjdkslfdsf   2')
    traits_view = View(
        'a',
        'b',
    )


c1 = C1()
c2 = C2()

import mlabscene
from PyQt5.QtCore import QThread


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(800, 600)
        self.centralwidget = QWidget()
        self.pushButton = QPushButton('haha', self.centralwidget)
        # self.pushButton = QPushButton('haha',self.centralwidget)
        self.pushButton.setGeometry(QRect(170, 120, 75, 23))
        self.pushButton.clicked.connect(self.pushButtonclick)
        # self.pushButton.setParent(self.centralwidget)
        self.menubar = QMenuBar(self)
        self.menubar.setGeometry(QRect(0, 0, 800, 23))
        self.setMenuBar(self.menubar)

        # self.horizontalLayout = QHBoxLayout(self.centralwidget)
        # self.horizontalLayout.addWidget(self.pushButton)

        self.statusbar = QStatusBar(self)
        self.setStatusBar(self.statusbar)

        self.menuFile = QMenu('file散射', self.menubar)
        self.menubar.addMenu(self.menuFile)

        self.actionNew = QAction('New0',self)
        self.actionNew.triggered.connect(self.triggered0)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='New')
        self.actionNew.triggered.connect(self.triggered)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='New1')
        self.actionNew.triggered.connect(self.triggered1)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='New-traits')
        self.actionNew.triggered.connect(self.triggered_traits)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='New-traits1')
        self.actionNew.triggered.connect(self.triggered_traits1)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='my widget')
        self.actionNew.triggered.connect(self.triggered_mywidget)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='mlab')
        self.actionNew.triggered.connect(self.triggered_mlab)
        self.menuFile.addAction(self.actionNew)

        self.actionNew = QAction(self, text='mlab fun')
        self.actionNew.triggered.connect(self.triggered_mlab_fun)
        self.menuFile.addAction(self.actionNew)



        # QMetaObject.connectSlotsByName(self)
        self.centralwidget1 = QWidget()
        self.pushButton1 = QPushButton(self.centralwidget1, text='haha1')
        self.pushButton1.setGeometry(QRect(340, 120, 75, 23))
        self.pushButton1.clicked.connect(self.haha)

        self.centralwidget2 = QWidget()
        self.pushButton2 = QPushButton(self.centralwidget2, text='haha2')
        self.pushButton2.setGeometry(QRect(720, 120, 75, 23))
        self.pushButton2.clicked.connect(self.haha2)

        # self.setCentralWidget(self.centralwidget2)
        # self.setCentralWidget(self.centralwidget2)
        self.setCentralWidget(self.centralwidget)

        self.mywidget = mywidget.MyWidget()

        self.mlabwidget=mlabscene.form.edit_traits().control
        self.mlabwidget.hide()

    def triggered0(self):
        print('fjdks111 triggered self 0')
        self.centralWidget().setParent(None)
        self.setCentralWidget(self.centralwidget)

    def triggered(self):
        print('fjdks111 triggered self')
        self.centralWidget().setParent(None)
        self.setCentralWidget(self.centralwidget1)

    def triggered1(self):
        print('fjdks111 triggered self 2')
        self.centralWidget().setParent(None)
        self.setCentralWidget(self.centralwidget2)

    def triggered_traits(self):
        print('fjdks111 triggered self traits')


        self.centralWidget().setParent(None)
        self.setCentralWidget(c1.edit_traits().control)

    def triggered_traits1(self):
        print('fjdks111 triggered self traits 1')
        self.centralWidget().setParent(None)
        self.setCentralWidget(c2.edit_traits().control)

    def triggered_mywidget(self):
        print('fjdks111 triggered self traits 1')
        self.centralWidget().setParent(None)
        self.setCentralWidget(self.mywidget)

    def triggered_mlab(self):
        print('fjdks111 triggered self traits 1')
        self.centralWidget().setParent(None)
        self.setCentralWidget(self.mlabwidget)
        self.mlabwidget.show()
    def triggered_mlab_fun(self):
        print('fjdks111 triggered self traits 1')
        mlabscene.form.test()
        t=threading.Thread()
        # t=QThread()
        t.run=self.run
        t.start()

    def run(self):
        import numpy as np
        import matplotlib.pyplot as plt
        nx, ny = 100, 100
        e = np.zeros((nx + 1, ny + 1))
        hx, hy = 0, 0
        s, nd = .5, 20
        fig = plt.figure()
        graph = plt.imshow(e, vmin=-.02, vmax=.02)
        plt.colorbar()
        print('haha')
        for n in np.arange(200):
            hx -= s * np.diff(e[1:-1, :], 1, 1)
            hy += s * np.diff(e[:, 1:-1], 1, 0)
            e[1:-1, 1:-1] += s * (np.diff(hy, 1, 0) - np.diff(hx, 1, 1))
            e[nx // 2, ny // 2] += np.sin(2 * np.pi * n * s / nd)
            graph.set_data(e)
            plt.pause(.05)
            print(n)
        plt.show()
        print('hahaend')
    def pushButtonclick(self):
        print('fjdks111 hah')

    def haha(self):
        print('fjdks111 hah new')

    def haha2(self):
        print('fjdks111 hah new 2')

    def on_pushButton_clicked(self):
        print('fjdks111')


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
